# -*- coding: UTF-8 -*-
import scrapy
import messAround.util.help as util


# B站-热门排行榜
# https://www.bilibili.com/ranking
class BilibiliRankSpider(scrapy.Spider):

    name = 'bilibili_rank'

    allowed_domains = ['bilibili.com']

    start_urls = ['http://bilibili.com/']

    # 日排行
    def start_requests(self):
        urls = [
            {"category": 1, "path": "https://www.bilibili.com/ranking/all/0/0/1"},
            {"category": 3, "path": "https://www.bilibili.com/ranking/all/0/0/3"},
            {"category": 7, "path": "https://www.bilibili.com/ranking/all/0/0/7"},
            {"category": 30, "path": "https://www.bilibili.com/ranking/all/0/0/30"},
        ]

        for url in urls:
            yield scrapy.Request(url=url['path'], meta={'category': url['category']}, headers=util.default_headers,
                                 callback=self.parse)

    def parse(self, response):
        for index in range(1, 100):
            item_xpath = '//*[@id="app"]/div[1]/div/div[1]/div[2]/div[3]/ul/li[' + str(index) + ']/div[2]/div[2]'
            # 标题
            title = response.xpath(item_xpath + '/a/text()').get()
            # 地址
            link = response.xpath(item_xpath + '/a/@href').get()
            # 封面
            cover_xpath = '//*[@id="app"]/div[1]/div/div[1]/div[2]/div[3]/ul/li[7]/div[2]/div[1]/a/div/img/@src'
            cover = response.xpath(cover_xpath).get()
            # 作者
            author = response.xpath(item_xpath + '/div[1]/a/span/text()').get()
            # 作者空间 //space.bilibili.com/1532165
            author_space = response.xpath(item_xpath + '/div[1]/a/@href').get()
            # 作者ID
            author_id = author_space[author_space.rfind("/") + 1:]
            # 播放量
            play = response.xpath(item_xpath + '/div[1]/span[1]/text()').get()
            # 弹幕
            view = response.xpath(item_xpath + '/div[1]/span[2]/text()').get()
            yield util.make_data({
                'source': 1,
                'source_name': response.meta['category'],
                'no': index,
                'title': title,
                'full_title': title,
                'cover': cover,
                'link': link,
                'author': author,
                'author_id': author_id,
                'author_space': author_space,
                'play_count': play,
                'danmaku_count': view,
            })
